package com.xpalive.base.dto.result;

import com.xpalive.commons.Constants.RespCode;
import java.util.ArrayList;
import java.util.List;
import lombok.Data;

@Data
public class RespResult<T> {

    private T data;
    private boolean success;
    private RespCode respCode;
    private List<RespFailure> respFailureList = new ArrayList<>();

    private RespResult() {
    }

    public static <T> RespResult<T> ofSuccess(T data) {
        RespResult<T> respResult = new RespResult<>();
        respResult.setSuccess(true);
        respResult.setData(data);
        respResult.setRespCode(RespCode.S200);
        return respResult;
    }

    public static RespResult<Object> ofSuccess() {
        RespResult<Object> respResult = new RespResult<>();
        respResult.setSuccess(true);
        respResult.setRespCode(RespCode.S200);
        return respResult;
    }

    public static <T> RespResult<T> ofFailure(RespCode respCode, String bizCode, String message) {
        RespResult<T> respResult = new RespResult<>();
        respResult.setSuccess(false);
        respResult.setRespCode(respCode);
        respResult.addRespFailure(bizCode, message);
        return respResult;
    }

    public static <T> RespResult<T> ofFailure(RespCode respCode, String message) {
        RespResult<T> respResult = new RespResult<>();
        respResult.setSuccess(false);
        respResult.setRespCode(respCode);
        respResult.addRespFailure(message);
        return respResult;
    }

    public static <T> RespResult<T> ofFailure(RespCode respCode, Throwable throwable) {
        return ofFailure(respCode, null, throwable);
    }

    public static <T> RespResult<T> ofFailure(RespCode respCode, String message,
        Throwable throwable
    ) {
        RespResult<T> respResult = new RespResult<>();
        respResult.setSuccess(false);
        respResult.setRespCode(respCode);
        if (message != null) {
            respResult.addRespFailure(message);
        }
        respResult.addRespFailure(throwable.getClass().getCanonicalName(), throwable.getMessage());
        return respResult;
    }

    public static <T> RespResult<T> ofFailure(String message) {
        RespResult<T> respResult = new RespResult<>();
        respResult.setSuccess(false);
        respResult.setRespCode(RespCode.E500);
        respResult.addRespFailure(message);
        return respResult;
    }

    public void addRespFailure(String bizCode, String bizMsg) {
        respFailureList.add(new RespFailure(bizCode, bizMsg));
    }

    public void addRespFailure(String bizMsg) {
        respFailureList.add(new RespFailure(bizMsg));
    }


}
